﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using System.Configuration;
using Infrastructure.Web.Extensions;
using Microsoft.Practices.Unity;
using Microsoft.Practices.Unity.Configuration;


namespace Skynet.MVCWebSite
{    
    public class MvcApplication : System.Web.HttpApplication
    {
        //string originalPath = Request.Path;
        //HttpContext.Current.RewritePath(Request.ApplicationPath, false);
        //IHttpHandler handler = new MvcHttpHandler();
        //handler.ProcessRequest(HttpContext.Current);
        //HttpContext.Current.RewritePath(originalPath, false);

        public static void RegisterGlobalFilters(GlobalFilterCollection filters)
        {
            filters.Add(new HandleErrorAttribute());
        }

        public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
            routes.MapRoute(
                "Default", // Route name
                "{controller}/{action}/{id}", // URL with parameters
                new { controller = "Default", action = "Index", id = UrlParameter.Optional } // Parameter defaults
            );

        }

        protected void Application_Start()
        {

            AreaRegistration.RegisterAllAreas();
            RegisterGlobalFilters(GlobalFilters.Filters);
            RegisterRoutes(RouteTable.Routes);
            RouteTable.Routes.MapPageRoute("SupportErrorMessage", "Support/Messages/ErrorMessageView.aspx", "~/Support/Messages/ErrorMessageView.aspx", true);


            var container = HttpContext.Current.Application.GetContainer();
            var AspectPath = Server.MapPath("~/Config/Aspect.config");
            var AspectMap = new ExeConfigurationFileMap() { ExeConfigFilename = AspectPath };
            var AspectSection = System.Configuration.ConfigurationManager.OpenMappedExeConfiguration(AspectMap, ConfigurationUserLevel.None).GetSection("unity") as UnityConfigurationSection;
            container.LoadConfiguration(AspectSection);

            var DataAccessPath = Server.MapPath("~/Config/DataAccess.config");
            var DataAccessMap = new ExeConfigurationFileMap() { ExeConfigFilename = DataAccessPath };
            var DataAccessSection = System.Configuration.ConfigurationManager.OpenMappedExeConfiguration(DataAccessMap, ConfigurationUserLevel.None).GetSection("unity") as UnityConfigurationSection;
            container.LoadConfiguration(DataAccessSection);


            var BusinessPath = Server.MapPath("~/Config/Business.config");
            var BusinessMap = new ExeConfigurationFileMap() { ExeConfigFilename = BusinessPath };
            var BusinessSection = System.Configuration.ConfigurationManager.OpenMappedExeConfiguration(BusinessMap, ConfigurationUserLevel.None).GetSection("unity") as UnityConfigurationSection;
            container.LoadConfiguration(BusinessSection);

            var AdministracionPath = Server.MapPath("~/Config/Administracion.config");
            var AdministracionMap = new ExeConfigurationFileMap() { ExeConfigFilename = AdministracionPath };
            var AdministracionSection = System.Configuration.ConfigurationManager.OpenMappedExeConfiguration(AdministracionMap, ConfigurationUserLevel.None).GetSection("unity") as UnityConfigurationSection;
            container.LoadConfiguration(AdministracionSection);

            var translator = container.Resolve<Infrastructure.Aspect.Services.Traslator.IEntityTranslatorService>();
    


            ControllerBuilder.Current.SetControllerFactory(container.Resolve<UnityControllerFactory>());

           
            
        }
    }
}